<template>
  <el-container class="content">
    <el-header>
      <div class="title">
        <img src="../assets/heima.png" alt>
        <p>黑马后台管理系统</p>
      </div>
      <el-button @click="logout" type="info">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="iszhedie?'64px':'200px'">
        <div class="toggle" @click="dian">|||</div>
        <!-- 侧边栏 -->
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="iszhedie"
          :collapse-transition="false"
          router
          :default-active="activepath" 
        >   
        <!-- default-active='/users'等于对应地址时默认对应的就高亮 -->
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <!-- 一级图表 -->
              <i :class="iconobj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="qiehuanpath('/'+subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-document"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      activepath: "",
      iszhedie: false,
      menulist: [],
      iconobj: {
        //   id:图标类名
        "125": "el-icon-user",
        "103": "el-icon-setting",
        "101": "el-icon-s-shop",
        "102": "el-icon-s-order",
        "145": "el-icon-star-on"
      }
    };
  },
  created() {
    //   发送ajax，一般喜欢写一个方法发ajax，然后在此调用
    this.getmenulist();
    this.activepath=window.sessionStorage.getItem("path");
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getmenulist() {
      // 发送ajax获取菜单数据
      let { data: res } = await this.$http.get("menus");
      console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      //   成功就把数据重新赋值
      this.menulist = res.data;
    },
    dian() {
      this.iszhedie = !this.iszhedie;
    },
    qiehuanpath(path){
        // console.log(path)
        this.activepath=path;
        // window.sessionStorage.setItem("path",path)
    }
  }
};
</script>
<style lang="less" scoped>
.title {
  display: flex;
  align-items: center; //侧轴居中
  p {
    color: #fff;
    margin-left: 20px;
  }
}
.content {
  height: 100%;
}
.el-header {
  background: #2b4b6b;
}
.el-aside {
  background: pink;
}
.el-button {
  position: absolute;
  right: 0px;
  top: 8px;
}
.toggle {
  background: grey;
  color: #fff;
  font-size: 12px;
  text-align: center;
  line-height: 30px;
  letter-spacing: 0.2rem;
  cursor: pointer;
}
</style>


